var parentHref = parent.location.href;
if(false&&(parentHref === null || parentHref.indexOf('manager/index') < 0) && parentHref.indexOf('manager/tagGoods') < 0){
	window.location.href = '../manager/error';
}
var noDataMessage = '<div style="padding:30px;">已经非常努力了，但是仍然找不到您想要的结果！尝试减少或改变搜索要求。</div>';

/**
* 基本订单列表对象类定义
* Functions {
	setParams : 设置查询参数，子类继承之后，可以通过sub.prototype.setParams = function(){}来覆盖，方法在内部调用
	setPages : 当查询结束，在success方法执行结束后，设置分页
	success : 查询结束并成功后回调此方法，在此方法默认中执行的是对结果集进行判断，然后显示到id为lsitId的html元素中,
	search : 执行查询方法，主要是$.ajax,
	list : 显示某一页结果,
	init : 初始化一些html元素以及事件绑定
  }
* 以上function，当子类继承了父类以后，可以通过覆盖来实现不同的执行过程
*/


/**
* 基本订单列表对象
* @path 接口url
* @listId 显示结果的html元素id
* @pageNumId 显示结果分页的html元素id
* @model 查询模式，detail和simple
* @ordertype 订单类型：一般订单，问题单，挂起单，合并单，客服待处理单...
*/
function ListObject(url, listId, pageNumId) {
	this.cp = 1;
	this.pp = 15;
	this.total = 0;
	this.pages = 0;
	this.paramObj = {};
	this.paramString = '';
	this.listId = listId;
	this.pageId = pageNumId;
	this.url = url;
}

/**
* 设置查询参数
* 注意json对象中，string类型的value部分需要额外加引号
*/
ListObject.prototype.setParams = function() {
	this.paramObj.cp = this.cp;
	this.paramObj.pp = this.pp;
	
	this.paramObj.keyword = v('#keyword');
	this.paramObj.beginTime = v('#bdate');
	this.paramObj.endTime = v('#edate');
	this.paramObj.sort = v('#sortInput');
	this.paramObj.cateId = v('#allCateSelect');
};
	
/**
* 设置结果的分页
*/
ListObject.prototype.setPages = function() {
	if($('#'+this.pageId).length < 1){
		return;
	}
	this.pages = Math.ceil(this.total/this.pp);
	var tPages = this.pages;
	var ttt = this.total;
	var cpp = this.cp;
	var ppp = this.pp;
	$('#'+this.pageId).html(getPage(cpp, ppp, ttt, 'ListData'));
};


function getPage(cp, pp, total, func) {
	if(total <= 0) {
		return '';
	}
	var h = '';
	var deltPage = 4;
	var totalPage = Math.ceil(total/pp);
	var firstPage = cp > 4 ? cp - 4 : 1;
	var maxPage = cp + 4 < totalPage ? cp + 4 : totalPage;
	var currentPage = cp;
	h += '<span style="float:left;margin-top:10px;margin-right:5px;">共 ' + total + ' 条 '+ totalPage +' 页记录</span>';
	if(currentPage != 1) {
		h += '<span class="pageOff" style="width:36px;" onclick="'+ func +'(' + 1 +');">首页</span>';
	}
	for(var i = firstPage; i <= maxPage; i += 1) {
		var v = i+"";
		if(v.length < 2){
			v = '<span style="padding-left:3px;padding-right:3px;">' + v + '</span>';
		}
		if(i == currentPage){
			h += '<span class="pageOn" >' + v + '</span>';
		}else{
			h += '<span class="pageOff" onclick="'+ func +'(' + i +');">' + v + '</span>';
		}
	}
	
	if(totalPage > currentPage) {
		h += '<span class="pageOff" style="width:36px;" onclick="'+ func +'(' + totalPage +');">尾页</span>';
	}
	return h;
}
	
/**
* ajax请求成功回调该方法
*/
ListObject.prototype.success = function(json) {
	var h = '';
	if(json !== null && json.length > 4){
		var obj1 = eval(json)[0];
		this.total = obj1.count;
		var list = obj1.list;
		h = '<table id="listTable" border="0" >' +
			'	<tr>' +
			'		<td class="center title"><span id="selectAll">选择</span></td>' +
			'		<td class="title sort" onclick="sortData(\'keyWord\');">关键字</td>' +
			'		<td class="center title">类别</td>' +
			'		<td class="center title sort" onclick="sortData(\'totalCount\');">被搜次数</td>' +
			'		<td class="center title sort" onclick="sortData(\'dailyCount\');">日统计</td>' +
			'		<td class="center title sort" onclick="sortData(\'weeklyCount\');">周统计</td>' +
			'		<td class="center title sort" onclick="sortData(\'monthlyCount\');">月统计</td>' +
			'		<td class="center title sort" onclick="sortData(\'rsCount\');">搜索结果数</td>' +
			'		<td class="center title sort" onclick="sortData(\'addTime\');">添加时间</td>' +
			'		<td class="center title sort">操作</td>' +
			'	</tr>';
		for(var i = 0, len = list.length; i < len; i ++){
			var o = list[i];
			var kid = o.id;
			var kw = o.keyWord || '';
			var cn = o.cate && o.cate.name || '';
			var tc = o.totalCount || 0;//总搜索数
			var dlyc = o.dailyCount || 0;//日统计
			var wc = o.weeklyCount || 0;//周统计
			var mc = o.monthlyCount || 0;//月统计
			var rc = o.rsCount || 0;//结果数
			var atime = o.addTimeStr || '';
			h +='	<tr class="bgTr">' +
				'		<td class="center"><input type="checkbox" class="checkBoxes" value='+kid+' /></td>' +
				'		<td class="moreOptTd"><a href="javascript:void(0);" class="moreOpt">'+kw+'</a></td>' +
				'		<td class="center">'+cn+'</td>' +
				'		<td class="center">'+tc+'</td>' +
				'		<td class="center">'+dlyc+'</td>' +
				'		<td class="center">'+wc+'</td>' +
				'		<td class="center">'+mc+'</td>' +
				'		<td class="center">'+rc+'</td>' +
				'		<td class="center">'+atime+'</td>' +
				'		<td class="center">' +
				'			<a href="#" class="aEdit">编辑</a>' +
				'			<a href="javascript:void(0);" onclick="deleteData(\''+kid+'\');" class="aDelete">删除</a>' +
				'		</td>' +
				'	</tr>';
		}
		if(list.length < 1){
			h += '<tr><td colspan="8">'+noDataMessage+'</td></tr>';
		}
		h += '</table>';
		$('#'+this.listId).html(h);
	}else{
		h = noDataMessage;
		$('#'+this.listId).html(h);
	}
};

/**
* 获取结果集,第n页的数据
*/
ListObject.prototype.search = function(n, fun1) {
	this.cp = n && n > 0 && n || this.cp || 1;
	this.paramObj.cp = this.cp;
	this.paramObj.pp = this.pp;
	var param = getParam(this.paramObj);
	var url = this.url + '?' + param;
	var o = this;
	url = encodeURI(url); 
	jQuery.ajax({
		type : 'POST',
		url : url,
		success : function(json){
			o.success(json);
			o.setPages();
			fun1();
		},
		error : function(){
		}
	});
};

/**
*列出结果,第n页的数据
*/
ListObject.prototype.list = function(n, fun1) {
	fun1 = fun1||function(){};
	this.setParams();
	this.search(n, fun1);
	//this.setPages();
};
/**
*初始化
*/
ListObject.prototype.init = function(){
	$('#selectAll').live('click', function(){
		var o = $(this);
		if(o.data('slc') && o.data('slc') == 1){
			$('input[type="checkbox"][class*="checkBoxes"]').attr('checked', false);
			o.data('slc', 0);
		}else{
			$('input[type="checkbox"][class*="checkBoxes"]').attr('checked', true);
			o.data('slc', 1);
		}
	});
	$('#listTable tr.bgTr').live('mouseover', function(){
		$(this).css('background-color', '#E6F3F8');
	}).live('mouseout', function(){
		$(this).css('background-color', 'white');
	});
};

function jsonToString(o){
	if(o.constructor == Array){
		var h = '[';
		for(var i = 0; i < o.length; i += 1){
			var obj = o[i];
			h += jsonToString(obj);
			if(i < o.length - 1){
				h += ',';
			}
		}
		h += ']';
		return h;
	}else{
		return '{'+jts(o)+'}';
	}
}
function jts(o){
	var s = '';
	for(var i in o){
		if(typeof o[i] == 'object'){
			s += i + ':{'+jts(o[i])+'},';
		}else{
			s += i + ':\'' + o[i] + '\',';
		}
	}
	if(s.charAt(s.length - 1) == ','){
		s = s.substring(0, s.length - 1);
	}
	return s;
}
function getParam(obj){
	var p = '';
	for(var i in obj){
		p += i + '=' + obj[i] + '&';
	}
	return p;
}
function v(id, dv){
	dv = dv || '';
	var vl = $(id).val();
	if(vl != null && vl.length > 0){
		return vl;
	}
	return dv;
}
function ajax(url, func){
	if(url && url.length > 0){
		var f = func || function(){};
		url += '&randomd='+new Date().getTime();
		jQuery.ajax({
			url : url,
			type : 'get',
			success : function(rs){
				func(rs);
			}
		});
	}
}
function reloadPage(){
	window.document.location.reload();
}

jQuery(function(){
	if($('#cateList').length > 0 && window.location.href.indexOf('cateIndex') < 0){
		ajax('cateinfo?t='+ $('#CATEINFOTYPE').val(), function(rs){
			if(rs && rs.length > 2){
				var list = eval(rs);
				var sh = '<option value="">全部</option>', ch = '';
				for(var i = 0, len = list.length; i < len; i += 1){
					var o = list[i];
					var id = o.id;
					var name = o.name;
					if(name == '全部'){
						name = '未分类';
					}else{
						ch += '<span class="cateClass">'+name+'</span>';
					}
					sh += '<option value="'+id+'">'+name+'</option>';
				}
				$('#allCateSelect,#baseWordEditorSelect').html(sh);
				$('#cateList').html(ch);
			}
		});
		$('#cateTitle').html($('#cateTitleInput').val());
	}
});
function addCateInfo(){
	var n = $('#cateName').val();
	if(n.toString().trim().length < 1){
		alert('请输入类别名称');
		return;
	}
	var cate = {};
	cate.name = n;
	cate.type = $('#CATEINFOTYPE').val();
	var str = jsonToString(cate);
	str = encodeURI(str);
	ajax('catesave?cateStr='+str, function(r){
		if(r == 1){
			reloadPage();
		}else{
			alert('新增类别失败，可能是该名称已存在或者名称过长');
			$('#cateName').focus();
		}
	});
}

function initCateSelect(type, sid) {
	ajax('cateinfo?t=' + type, function(rs){
		if(rs && rs.length > 2){
			var list = eval(rs);
			var sh = '<option value="">全部</option>';
			for(var i = 0, len = list.length; i < len; i += 1){
				var o = list[i];
				var id = o.id;
				var name = o.name;
				sh += '<option value="'+id+'">'+name+'</option>';
			}
			$('#' + sid).html(sh);
		}
	});
}

String.prototype.trim = function() {
	var val = this;
	return val.replace(/\s*/g, '');
}
function showRs(s){
	$('#batchResult').stop(true).fadeOut(0).html(s).fadeIn(1000,function(){
		$(this).fadeOut(500);
	});
}
function getids(){
	var chboxes = $('input[type="checkbox"][class="checkBoxes"]').filter(':checked');
	if(chboxes.length <= 0){
		alert("请选择数据。");
		return '';
	}
	var ids = chboxes.map(function(){
		return $(this).val();
	}).get().join(',');
	return ids;
}

//批量指定类别弹出
function batchCate(){
	var ids = getids();
	if(ids == null || ids.length < 1){
		return;
	}
	$('#cateInfoHTML').html('').append($('#allCateSelect').clone());
	$('#selectCateFloat').showPop();
}